package com.th.supcom.pds.impl.service.internal.config;

import java.util.List;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PhamMachineConfig;
import com.th.supcom.pds.domain.pojo.PhamWindowConfig;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.manager.PhamMachineConfigManager;
import com.th.supcom.pds.internal.api.config.PhamMachineConfigService;
import com.th.supcom.pds.internal.api.config.PhamWindowConfigService;

public class PhamMachineConfigServiceImpl implements PhamMachineConfigService
{
    private PhamMachineConfigManager m_phamMachineConfigManager;
    private TranslatorManager m_translatorManager;
    private PhamWindowConfigService m_phamWindowConfigService;

    public void setPhamWindowConfigService (PhamWindowConfigService phamWindowConfigService)
    {
        m_phamWindowConfigService = phamWindowConfigService;
    }

    public void setPhamMachineConfigManager (PhamMachineConfigManager phamMachineConfigManager)
    {
        m_phamMachineConfigManager = phamMachineConfigManager;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    @Override
    public void addPhamMachineConfig (PhamMachineConfig phamMachineConfig)
    {
        s_logger.info ("修调addPhamMachineConfig用参数：phamMachineConfig:{}", new Object[]
        { phamMachineConfig });
        if (null == phamMachineConfig)
        {
            throw new IllegalArgumentException ("添加药房机器属性时，保存对象不能为空");
        }
        this.validatePhamMachineConfig (phamMachineConfig);
        this.checkPhamMachineConfigExistence (phamMachineConfig, true);
        try
        {
            m_phamMachineConfigManager.addPhamMachineConfig (phamMachineConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("添加药房机器属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("添加药房机器属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /**
     * 校验药房机器属性
     * 
     * @param phamMachineConfig
     */
    private void validatePhamMachineConfig (PhamMachineConfig phamMachineConfig)
    {
        if (StringUtils.isEmpty (phamMachineConfig.getDeptCode ()))
        {
            throw new IllegalArgumentException ("所属药房不能为空");
        }
        if (StringUtils.isEmpty (phamMachineConfig.getMachineName ()))
        {
            throw new IllegalArgumentException ("机器名称不能为空");
        }
        if (StringUtils.isEmpty (phamMachineConfig.getMachineType ()))
        {
            throw new IllegalArgumentException ("机器类型不能为空");
        }
        if (StringUtils.isEmpty (phamMachineConfig.getServiceSite ()))
        {
            throw new IllegalArgumentException ("服务地址不能为空");
        }
    }

    /**
     * 校验数据库中是否已存在当前机器配置
     * 
     * @param phamMachineConfig
     * @param isAdd 添加时，true；更新时，false
     */
    private void checkPhamMachineConfigExistence (PhamMachineConfig phamMachineConfig, boolean isAdd)
    {
        PhamMachineConfig condition = new PhamMachineConfig ();
        condition.setDeptCode (phamMachineConfig.getDeptCode ());
        if (null != phamMachineConfig.getMachineName ())
        {
            condition.setMachineName (phamMachineConfig.getMachineName ());
        }
        int count = this.findPhamMachineConfigCountByExample (condition);
        if (isAdd)
        {
            if (count != 0)
            {
                throw new BusinessException ("该药房机器属性配置已存在");
            }
            return;
        }
        if (count > 1)
        {
            throw new BusinessException ("该药房机器属性配置已存在");
        }
    }

    @Override
    public void modifyPhamMachineConfig (PhamMachineConfig phamMachineConfig)
    {
        s_logger.info ("修调modifyPhamMachineConfig用参数：phamMachineConfig:{}", new Object[]
        { phamMachineConfig });
        if (null == phamMachineConfig)
        {
            throw new IllegalArgumentException ("更新药房机器属性列表时，对象不能为空");
        }
        this.validatePhamMachineConfig (phamMachineConfig);
        this.checkPhamMachineConfigExistence (phamMachineConfig, false);
        try
        {
            m_phamMachineConfigManager.modifyPhamMachineConfig (phamMachineConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药房机器属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药房机器属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }

    }

    @Override
    public void modifyPhamMachineConfigByExample (PhamMachineConfig phamMachineConfig)
    {
        s_logger.info ("修调modifyPhamMachineConfigByExample用参数：phamMachineConfig:{}", new Object[]
        { phamMachineConfig });
        if (null == phamMachineConfig)
        {
            throw new IllegalArgumentException ("更新药房机器属性列表时，对象不能为空");
        }
        this.checkPhamMachineConfigExistence (phamMachineConfig, false);
        try
        {
            m_phamMachineConfigManager.modifyPhamMachineConfigByExample (phamMachineConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药房机器属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药房机器属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /**
     * 查找符合条件
     * 
     * @param printerId
     * @param machineId
     * @return
     */
    private boolean validatePrinter (Long printerId, Long machineId)
    {
        if (printerId != null && printerId.equals (machineId))
        {
            return true;
        }
        return false;
    }

    @Override
    public void removePhamMachineConfig (PhamMachineConfig phamMachineConfig)
    {
        s_logger.info ("修调removePhamMachineConfig用参数：phamMachineConfig:{}", new Object[]
        { phamMachineConfig });
        if (null == phamMachineConfig || null == phamMachineConfig.getPhamMachineConfigId ())
        {
            throw new IllegalArgumentException ("删除药房机器属性列表时，对象不能为空");
        }
        checkMachineInUsage (phamMachineConfig);
        try
        {
            m_phamMachineConfigManager.removePhamMachineConfigByID (phamMachineConfig.getPhamMachineConfigId ());
        }
        catch (Exception e)
        {
            s_logger.error ("删除药房机器属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("删除药房机器属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /**
     * 检验机器是否在使用中
     * 
     * @param phamMachineConfig
     * @return
     */
    private void checkMachineInUsage (PhamMachineConfig phamMachineConfig)
    {
        PhamMachineConfig dbConfig = this.getPhamMachineConfigById (phamMachineConfig.getPhamMachineConfigId ());
        PhamWindowConfig phamWindowConfig = new PhamWindowConfig ();
        phamWindowConfig.setDeptCode (dbConfig.getDeptCode ());
        /*
         * List <PhamWindowConfig> windowList =
         * m_phamWindowConfigService.findPhamWindowConfigByExample
         * (phamWindowConfig); for (PhamWindowConfig phamWindowConfig2 :
         * windowList) { if (validatePrinter (phamWindowConfig2.getSlipPrinter
         * (), phamMachineConfig.getPhamMachineConfigId ()) || validatePrinter
         * (phamWindowConfig2.getDispensePrinter (),
         * phamMachineConfig.getPhamMachineConfigId ()) || validatePrinter
         * (phamWindowConfig2.getNotDispensePrinter (),
         * phamMachineConfig.getPhamMachineConfigId ()) || validatePrinter
         * (phamWindowConfig2.getRefundApplyPrinter (),
         * phamMachineConfig.getPhamMachineConfigId ()) || validatePrinter
         * (phamWindowConfig2.getWithdrawlPrinter (),
         * phamMachineConfig.getPhamMachineConfigId ())) { throw new
         * BusinessException (dbConfig.getMachineName () + "正在使用中，无法删除"); } }
         */
    }

    @Override
    public PhamMachineConfig getPhamMachineConfigById (Long phamMachineConfigId)
    {
        s_logger.info ("修调getPhamMachineConfigById用参数：phamMachineConfigId:{}", new Object[]
        { phamMachineConfigId });
        if (null == phamMachineConfigId)
        {
            throw new IllegalArgumentException ("通过主键查询药房机器属性时，主键不能为空");
        }
        try
        {
            PhamMachineConfig phamMachineConfig = m_phamMachineConfigManager.getPhamMachineConfigByID (phamMachineConfigId);
            phamMachineConfig = m_translatorManager.translate (phamMachineConfig);
            return phamMachineConfig;
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键查询药房机器属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键查询药房机器属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public List <PhamMachineConfig> findPhamMachineConfigByExample (PhamMachineConfig phamMachineConfig)
    {
        s_logger.info ("修调findPhamMachineConfigByExample用参数：phamMachineConfig:{}", new Object[]
        { phamMachineConfig });
        if (null == phamMachineConfig)
        {
            throw new IllegalArgumentException ("查询药房机器属性列表时，查询条件不能为空");
        }
        try
        {
            List <PhamMachineConfig> list = m_phamMachineConfigManager.findPhamMachineConfigListByExample (phamMachineConfig);
            list = m_translatorManager.translate (list);
            return list;
        }
        catch (Exception e)
        {
            s_logger.error ("查询药房机器属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药房机器属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public int findPhamMachineConfigCountByExample (PhamMachineConfig phamMachineConfig)
    {
        s_logger.info ("修调findPhamMachineConfigCountByExample用参数：phamMachineConfig:{}", new Object[]
        { phamMachineConfig });
        if (null == phamMachineConfig)
        {
            throw new IllegalArgumentException ("查询药房机器属性列表时，查询条件不能为空");
        }
        try
        {
            int count = m_phamMachineConfigManager.findPhamMachineConfigCountByExample (phamMachineConfig);
            return count;
        }
        catch (Exception e)
        {
            s_logger.error ("查询药房机器属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药房机器属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamMachineConfigServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
